import axios from 'axios'
import qs from 'qs'
import store from '@/store/store.js'
import global from '@/utils/global.js'
// axios 配置
axios.defaults.timeout = 5000;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
axios.defaults.baseURL =global.API_HOST;
axios.interceptors.request.use(function(config){
	if (config.method == 'post') {
		config.data = qs.stringify(config.data);
	}
	if (config.url.indexOf("login")==-1) {
		let token = window.$cookies.get("token");
		config.headers.token = token;
	}
	return config;
},function(error){
	return Promise.reject(error);
});
axios.interceptors.response.use(function(response){
	if ((response.data.code =="400" || response.data.code ==400)&&response.config.url.indexOf("logout")==-1) {
		store.commit({
          type:"setShowLogin",
          showLogin:true
        });
        return false;
	}else{
		window.$cookies.set('token',window.$cookies.get("token"),3600);
		return response;
	}
},function(error){
	return Promise.reject(error);
});
export function fetch(method,url, params) {
  return new Promise((resolve, reject) => {
    if (method == "post") {
    	axios.post(url, params)
	      .then(response => {
	        resolve(response);
	      }, err => {
	        reject(err);
	      })
	      .catch((error) => {
	        reject(error)
	      });
    }else{
    	axios.get(url, params)
	      .then(response => {
	        resolve(response);
	      }, err => {
	        reject(err);
	      })
	      .catch((error) => {
	        reject(error)
	      });
    }
  })
};
export function fetch2(method,url, params) {
  return new Promise((resolve, reject) => {
    if (method == "post") {
      axios.post(url,params,{headers: {'Content-Type': 'application/json;charset=UTF-8'}})
        .then(response => {
          resolve(response);
        }, err => {
          reject(err);
        })
        .catch((error) => {
          reject(error)
        });
    }else{
      axios.get(url, params)
        .then(response => {
          resolve(response);
        }, err => {
          reject(err);
        })
        .catch((error) => {
          reject(error)
        });
    }
  })
}

export default {
	//登录
  loginCheck(userName,userPwd) {
    return fetch('post','/login',{
    	userName: userName,
    	userPwd: userPwd
    });
  },
	//退出
  logOut(){
  	return fetch('post',"/api/logout");
  },
	//获取主诉
  queryComplaints(){
  	return fetch('get',"/api/queryComplaints");
  },
  //获取主诉症状
  querySymptoms(complaintCode) {
    return fetch('post','/api/querySymptoms',{
    	complaintCode: complaintCode
    });
  },
  	//获取科室信息
  queryDepts(hospitalCode) {
    return fetch('post','/api/queryDepts',{
    	hospitalCode: hospitalCode
    });
  },
  //获取可选医生信息
  queryDoctors(keyWords,hospitalCode) {
    return fetch('post','/api/queryDoctors',{
    	keyWords: keyWords,
    	hospitalCode: hospitalCode
    });
  },
  //疾病诊断
  diagnosisDisease(complaintCode,groupContent,light) {
    return fetch('post','/api/diagnosis',{
    	complaintCode: complaintCode,
    	groupContent: groupContent,
    	light: light
    });
  },
  //获取检查类
  getAllExamCategory(){
    return fetch('get',"/api/getAllExamCategory");
  },
  //根据检查类获取检查内容
  getExamByCategory(examCategoryName){
    return fetch('post','/api/getExamByCategory',{
      examCategoryName: examCategoryName
    });
  },
  //根据关键字查找药品
  getDrugs(keyWords){
    return fetch('post','/api/getDrugs',{
      keyWords: keyWords
    });
  },
  //根据生命体征组合号，查询护理分诊
  getNurseDisposal(lifeGroup){
    return fetch('post','/api/getNurseDisposal',{
      lifeGroup: lifeGroup
    });
  },
  //获取可选的护理诊断内容
  queryNurseDiagnosis(keyWords,hospitalCode) {
    return fetch('post','/api/nurseDiagnosis',{
      keyWords: keyWords,
      hospitalCode:hospitalCode
    });
  },
  //获取诊断处置
  getDoctorDisposal(diseaseCodes,tDiseaseCode,light,allSymptoms) {
    return fetch('post','/api/getDoctorDisposal',{
      diseaseCodes: diseaseCodes,
      tDiseaseCode: tDiseaseCode,
      light: light,
      allSymptoms: allSymptoms
    });
  },
  //保存记录
  addRecord(record) {
    return fetch('post','/api/addRecord',record);
  },
  //查询待诊患者
  getWaits(user) {
    return fetch('post','/api/getWaits',user);
  },
  //查询单个患者详情
  getRecordByUUID(UUID) {
    return fetch('post','/api/getRecordByUUID',{
      recordUUID:UUID
    });
  },
  //查询分诊记录列表
  getTriage(startTime,lastTime,hospitalCode) {
    return fetch('post','/api/getTriage',{
      lastTime:lastTime,
      startTime:startTime,
      hospitalCode:hospitalCode
    });
  },
  //检查重名个数
  getRepeatCount(recordName) {
    return fetch('post','/api/getRepeatCount',{
      recordName:recordName
    });
  },
  //患者信息和时间节点关联
  setNodeUUID(recordUUID,nodeUUID) {
    return fetch('post','/api/setNodeUUID',{
      recordUUID:recordUUID,
      nodeUUID:nodeUUID
    });
  },
  //根据nodeUUID查询全部节点信息
  queryNode(nodeUUID) {
    return fetch('post','/api/queryNode',{
      nodeUUID:nodeUUID
    });
  },
  //处理node操作
  handleNode(node) {
    return fetch('post','/api/handleNode',node);
  },
  //根据医院查询处置地点信息
  queryAllPlace(hospitalCode) {
    return fetch('post','/api/queryAllPlace',{
      hospitalCode:hospitalCode
    });
  },
  //刷卡
  swingCard(cardId,hospitalCode) {
    return fetch('post','/api/swingCard',{
      cardId:cardId,
      hospitalCode:hospitalCode
    });
  },
  //添加修改后的患者病历
  addDoctorCase(doctorCase) {
    return fetch('post','/api/addDoctorCase',doctorCase);
  },
  //查询患者存在的病历
  queryDoctorCase(nodeUUID) {
    return fetch('post','/api/queryDoctorCase',{
      nodeUUID:nodeUUID
    });
  },
  //添加处方内容
  addRecipe(recipe) {
    return fetch('post','/api/addRecipe',recipe);
  },
  //查询处方
  queryRecipe(nodeUUID) {
    return fetch('post','/api/queryRecipe',{
      nodeUUID:nodeUUID
    });
  },
  //繁体翻译
  toTraditional(text) {
    return fetch('post','/toTraditional',{
      text:text
    });
  },
  //查询急救统计数据
  getRescueData(startDate,endDate,hospitalCode) {
    return fetch('post','/api/statistic/rescue',{
      startDate:startDate,
      endDate:endDate,
      hospitalCode:hospitalCode
    });
  },
  //查询患者统计数据
  getPatientData(startDate,endDate,hospitalCode) {
    return fetch('post','/api/statistic/patientinfo',{
      startDate:startDate,
      endDate:endDate,
      hospitalCode:hospitalCode
    });
  },
  //查询疾病统计数据
  getDiseaseData(startDate,endDate,hospitalCode) {
    return fetch('post','/api/statistic/disease',{
      startDate:startDate,
      endDate:endDate,
      hospitalCode:hospitalCode
    });
  },
  //查询胸痛统计数据
  getChestData(startDate,endDate,hospitalCode) {
    return fetch('post','/api/statistic/chest',{
      startDate:startDate,
      endDate:endDate,
      hospitalCode:hospitalCode
    });
  },
  //查询冠心病统计数据
  getCoronaryData(startDate,endDate,hospitalCode) {
    return fetch('post','/api/statistic/coronary',{
      startDate:startDate,
      endDate:endDate,
      hospitalCode:hospitalCode
    });
  },
  //查询科室统计数据
  getKsData(startDate,endDate,hospitalCode) {
    return fetch('post','/api/statistic/ks',{
      startDate:startDate,
      endDate:endDate,
      hospitalCode:hospitalCode
    });
  },
  //查询近7天统计数据
  getOutpatient(hospitalCode) {
    return fetch('post','/api/statistic/outpatient',{
      hospitalCode:hospitalCode
    });
  },
  //综合查询近3天的患者信息
  searchRecords(hospitalCode,searchWords,searchType) {
    return fetch('post','/api/searchRecord',{
      hospitalCode:hospitalCode,
      searchWords:searchWords,
      searchType:searchType
    });
  },
}